<?php

namespace backend;

use yii\base\BootstrapInterface;
use Yii;
use yii\web\Application;

/**
 * Users module bootstrap class.
 */
class Bootstrap implements BootstrapInterface
{

    /**
     * @var bool
     */
    public $rbacAuth = true;

    /**
     * @var bool
     */
    public $adminAccessLog = true;

    /**
     * @param \yii\web\Application $app
     * @inheritdoc
     */
    public function bootstrap($app)
    {
        if ($this->rbacAuth)
        {
            Yii::$app->on(Application::EVENT_BEFORE_ACTION, function ($event)
            {
                $adminAuth = Yii::$app->get('adminAuth');
                $adminAuth->adminAccessCheck($event);
            });
        }

        if ($this->adminAccessLog)
        {
            Yii::$app->on(Application::EVENT_BEFORE_ACTION, function ($event)
            {
                $adminAccessLog = Yii::$app->get('adminAccessLog');
                $adminAccessLog->addByAfterRequestEvent($event);
            });
        }

        // Add module URL rules.
        $app->urlManager->addRules(
            [
                '<_a:(login|signup|activation|recovery|recovery-confirmation|resend|fileapi-upload)>' => 'user/guest/<_a>',
                'login/<_a:(oauth-callback)>' => 'guest/<_a>',
            ],
            false
        );

        // Yii2的extensions.php已经定义了一次,所以只能在这里覆盖咯
        if ($app->has('i18n')) {
            $app->i18n->translations['audit'] = [
                'class' => 'yii\i18n\PhpMessageSource',
                'sourceLanguage' => 'en',
                'basePath' => '@backend/modules/audit/messages',
                'fileMap' => [
                    'audit' => 'audit.php',
                ]
            ];
        }
    }
}
